using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class NewMainMenu_Hypothetical_ContextMenu : MonoBehaviour
{
	public List<TextMeshProUGUI> BackgroundTextLines = new List<TextMeshProUGUI>();

	[SerializeField]
	private List<int> BackgroundTextMaxCount = new List<int>();

	[SerializeField]
	private NewMainMenu_HypotheticalSubMenu hypotheticalSubMenu;

	[SerializeField]
	private TextMeshProUGUI optionText;

	private Coroutine currentMainTextWriting;

	private int CurrentSelected;

	private int previousSelected = -1;

	public Hypothesis CurrentHypothesis;

	[SerializeField]
	private TextMeshProUGUI HypotheticalTitle;

	[SerializeField]
	private TextMeshProUGUI HypotheticalDescription;

	[SerializeField]
	private TextMeshProUGUI Warning;

	[SerializeField]
	private TextMeshProUGUI HypotheticalQuests;

	[SerializeField]
	private Image HypotheticalIcon;

	private bool AllTextShown;

	private bool wasEnabled;

	private bool AllowInput = true;

	private void Awake()
	{
		CurrentSelected = PlayerPrefs.GetInt("NewMainMenu_PreviousSelectedHypothetical", 0);
		foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
		{
			BackgroundTextMaxCount.Add(((TMP_Text)backgroundTextLine).text.Length);
			((TMP_Text)backgroundTextLine).maxVisibleCharacters = 0;
		}
	}

	private void Update()
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		if (AllowInput)
		{
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Right))
			{
				CurrentSelected++;
				CheckCurrentSelectedOutsideBounds();
				CheckUpdateScreenText();
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Left))
			{
				CurrentSelected--;
				CheckCurrentSelectedOutsideBounds();
				CheckUpdateScreenText();
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Confirm) || Input.GetKeyDown(PlayerInput.Instance.Key_Menu))
			{
				SelectOption();
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Cancel))
			{
				NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuDeny);
				hypotheticalSubMenu.AllowInput = true;
				((Component)this).gameObject.SetActive(false);
			}
		}
		if (!wasEnabled && ((Component)this).gameObject.activeSelf)
		{
			previousSelected = -1;
			wasEnabled = true;
			AllowInput = true;
			((MonoBehaviour)this).StartCoroutine(ShowBackgroundText());
			CheckUpdateScreenText();
			SetupHypothetical();
		}
	}

	private void SelectOption()
	{
		PlayerPrefs.SetInt("NewMainMenu_PreviousSelectedHypothetical", CurrentSelected);
		AllowInput = false;
		previousSelected = -1;
		NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuSelect);
		((MonoBehaviour)this).StartCoroutine(SelectOptionTimer());
		CheckUpdateScreenText();
	}

	private IEnumerator SelectOptionTimer()
	{
		yield return (object)new WaitForSeconds(0.5f);
		if (CurrentSelected == 0)
		{
			GonerMenu.CurrentActiveHypothesis = CurrentHypothesis;
			UI_LoadingIcon.ToggleLoadingIcon(showIcon: true);
			UI_FADE.Instance.StartFadeIn(CurrentHypothesis.HypothesisStartingScene, 0.5f);
			DEBUG_EnableMarkiplier.ChangeMarkiplierState(DEBUG_EnableMarkiplier.MarkiplierEmotions.CheekyGrin);
		}
		else
		{
			NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuDeny);
			hypotheticalSubMenu.AllowInput = true;
			((Component)this).gameObject.SetActive(false);
		}
	}

	private void CheckUpdateScreenText()
	{
		if (previousSelected != CurrentSelected)
		{
			previousSelected = CurrentSelected;
			string text = "";
			string[] array = new string[2] { "游玩 ", "返回" };
			for (int i = 0; i < array.Length; i++)
			{
				text = ((CurrentSelected != i) ? (text + array[i]) : (AllowInput ? (text + "<color=yellow>-> " + array[i] + "</color>") : (text + "<color=green>-> " + array[i] + "</color>")));
			}
			ChangeMainText(text, AllowAnimation: true);
		}
	}

	private void CheckCurrentSelectedOutsideBounds()
	{
		if (CurrentSelected < 0)
		{
			CurrentSelected = 1;
		}
		if (CurrentSelected > 1)
		{
			CurrentSelected = 0;
		}
		NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuMove);
	}

	private void SetupHypothetical()
	{
		//IL_0109: Unknown result type (might be due to invalid IL or missing references)
		((TMP_Text)HypotheticalTitle).text = CurrentHypothesis.HypothesisName;
		((TMP_Text)HypotheticalDescription).text = "<color=grey>猜想是这样的...\n\n</color>" + CurrentHypothesis.HypothesisDescription;
		if ((Object)(object)CurrentHypothesis.HypothesisMenuSprite != (Object)null && CurrentHypothesis.HypotheticalReleased)
		{
			HypotheticalIcon.sprite = CurrentHypothesis.HypothesisMenuSprite;
		}
		else
		{
			HypotheticalIcon.sprite = hypotheticalSubMenu.noIconSprite;
		}
		((TMP_Text)Warning).text = CurrentHypothesis.TimeWhenWritten;
		string text = "";
		text = ((CurrentHypothesis.HypothesisGoals.Length == 0) ? (text + "<color=red>【本猜想没有小任务】</color>") : (text + "<color=green>【可选有奖小任务】</color>"));
		HypothesisGoal[] hypothesisGoals = CurrentHypothesis.HypothesisGoals;
		foreach (HypothesisGoal hypothesisGoal in hypothesisGoals)
		{
			int num = PlayerPrefs.GetInt("HypothesisGoal_" + hypothesisGoal.GoalPlayprefName, 0);
			text += "\n";
			((Graphic)HypotheticalQuests).color = Color.white;
			if (hypothesisGoal.RequiredTasks > 1)
			{
				text = ((num >= hypothesisGoal.RequiredTasks) ? (text + "<color=yellow>") : (text + "<color=white>"));
				text = text + "(" + num + "/" + hypothesisGoal.RequiredTasks + ")";
			}
			else
			{
				text = ((num == 0) ? (text + "<color=white>(未完成)") : (text + "<color=yellow>(已完成)"));
			}
			text += hypothesisGoal.GoalHint;
		}
		((TMP_Text)HypotheticalQuests).text = text;
	}

	private void OnDisable()
	{
		wasEnabled = false;
		AllTextShown = false;
		ChangeMainText("", AllowAnimation: false);
		foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
		{
			((TMP_Text)backgroundTextLine).maxVisibleCharacters = 0;
		}
	}

	private IEnumerator ShowBackgroundText()
	{
		if (!SettingsManager.Instance.GetBoolSettingValue("SimpleVFX"))
		{
			List<TextMeshProUGUI> finishedTexts = new List<TextMeshProUGUI>();
			while (!AllTextShown)
			{
				yield return null;
				foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
				{
					if (!finishedTexts.Contains(backgroundTextLine) && ((TMP_Text)backgroundTextLine).maxVisibleCharacters >= ((TMP_Text)backgroundTextLine).text.Length)
					{
						finishedTexts.Add(backgroundTextLine);
					}
					else if (((TMP_Text)backgroundTextLine).maxVisibleCharacters < ((TMP_Text)backgroundTextLine).text.Length)
					{
						float num = 100 + Random.Range(-60, -10);
						((TMP_Text)backgroundTextLine).maxVisibleCharacters = ((TMP_Text)backgroundTextLine).maxVisibleCharacters + Mathf.CeilToInt(num * Time.deltaTime);
						((TMP_Text)backgroundTextLine).maxVisibleCharacters = Mathf.Clamp(((TMP_Text)backgroundTextLine).maxVisibleCharacters, 0, ((TMP_Text)backgroundTextLine).text.Length);
						NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuClick);
					}
				}
				if (CompareTextLists(finishedTexts, BackgroundTextLines))
				{
					AllTextShown = true;
					MonoBehaviour.print((object)"all text shown");
				}
			}
			yield break;
		}
		foreach (TextMeshProUGUI backgroundTextLine2 in BackgroundTextLines)
		{
			((TMP_Text)backgroundTextLine2).maxVisibleCharacters = ((TMP_Text)backgroundTextLine2).text.Length;
		}
	}

	private bool CompareTextLists(List<TextMeshProUGUI> list1, List<TextMeshProUGUI> list2)
	{
		if (list1.Count != list2.Count)
		{
			return false;
		}
		for (int i = 0; i < list1.Count; i++)
		{
			if (((TMP_Text)list1[i]).text != ((TMP_Text)list2[i]).text)
			{
				return false;
			}
		}
		return true;
	}

	public void ChangeMainText(string text, bool AllowAnimation)
	{
		((TMP_Text)optionText).text = text;
		if (!SettingsManager.Instance.GetBoolSettingValue("SimpleVFX") && AllowAnimation)
		{
			((TMP_Text)optionText).maxVisibleCharacters = 0;
			if (currentMainTextWriting != null)
			{
				((MonoBehaviour)this).StopCoroutine(currentMainTextWriting);
			}
			currentMainTextWriting = ((MonoBehaviour)this).StartCoroutine(ScrollMainText());
		}
		else
		{
			((TMP_Text)optionText).maxVisibleCharacters = text.Length;
		}
	}

	private IEnumerator ScrollMainText()
	{
		yield return null;
		while (((TMP_Text)optionText).maxVisibleCharacters < ((TMP_Text)optionText).text.Length)
		{
			float num = 600 + Random.Range(-120, 0);
			TextMeshProUGUI obj = optionText;
			((TMP_Text)obj).maxVisibleCharacters = ((TMP_Text)obj).maxVisibleCharacters + Mathf.CeilToInt(num * Time.deltaTime);
			((TMP_Text)optionText).maxVisibleCharacters = Mathf.Clamp(((TMP_Text)optionText).maxVisibleCharacters, 0, ((TMP_Text)optionText).text.Length);
			NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuClick);
			yield return null;
		}
	}
}
